package br.edu.ufcg.greengrid.workload;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public abstract class AbstractWorkloadParser {

	public AbstractWorkloadParser() {
		super();
	}
	
	protected void generateOutput(File outputFile, List<String> lines) throws IOException {

		if (outputFile.exists()) {
			throw new IllegalArgumentException(String.format("Error! Output file already exists %s!", outputFile
					.getAbsolutePath()));
		}

		BufferedWriter bw = null;
		
		try {
			bw = new BufferedWriter(new FileWriter(outputFile));

			for (String line : lines) {
				bw.write(line);
				bw.newLine();
			}

		} finally {
			if (bw != null) {
				bw.close();
			}
		}

	}
	
	protected List<File> getInputFiles(String... filesName) {
		return getInputFiles(new ArrayList<String>(Arrays.asList(filesName)));
	}
	
	protected List<File> getInputFiles(List<String> filesName) {

		List<File> inputs = new LinkedList<File>();
		File input = null;
		for (String fileName : filesName) {
			input = getInputFile(fileName);
			inputs.add(input);
		}

		return inputs;
	}
	
	protected File getInputFile(String fileName) {
		File input;
		input = new File(fileName);
		if (!input.exists()) {
			throw new IllegalArgumentException(String.format("Input file not found: %s", fileName));
		}
		return input;
	}
	
	protected File getOutputFile(String fileName) {
		File output;
		output = new File(fileName);
		if (output.exists()) {
			throw new IllegalArgumentException(String.format("Output file already exists: %s", output.getAbsolutePath()));
		}
		return output;
	}
	
}
